<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions({ dates, counts, approved } = {}) {
      // 默认数据
      const defaultDates = ['1/1', '1/2', '1/3', '1/4', '1/5', '1/6', '1/7']
      const defaultCounts = [0, 0, 0, 0, 0, 0, 0]
      const defaultApproved = [0, 0, 0, 0, 0, 0, 0]

      this.chart.setOption({
        xAxis: {
          data: dates || defaultDates,
          boundaryGap: false,
          axisTick: {
            show: false
          },
          axisLine: {
            lineStyle: {
              color: '#e6e6e6'
            }
          }
        },
        grid: {
          left: 20,
          right: 20,
          bottom: 20,
          top: 40,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          backgroundColor: 'rgba(255, 255, 255, 0.95)',
          borderColor: '#e6e6e6',
          textStyle: {
            color: '#333'
          },
          formatter: function(params) {
            let result = `<div style="font-weight: bold; margin-bottom: 5px;">${params[0].axisValue}</div>`
            params.forEach(param => {
              result += `
                <div style="margin: 2px 0;">
                  <span style="display: inline-block; width: 10px; height: 10px; background: ${param.color}; border-radius: 50%; margin-right: 5px;"></span>
                  ${param.seriesName}: ${param.value}
                </div>
              `
            })
            return result
          }
        },
        yAxis: {
          axisTick: {
            show: false
          },
          axisLine: {
            lineStyle: {
              color: '#e6e6e6'
            }
          },
          splitLine: {
            lineStyle: {
              color: '#f0f0f0'
            }
          }
        },
        legend: {
          data: ['预约数量', '审核通过'],
          top: 10,
          textStyle: {
            fontSize: 12
          }
        },
        series: [{
          name: '预约数量',
          itemStyle: {
            normal: {
              color: '#409EFF',
              lineStyle: {
                color: '#409EFF',
                width: 3
              }
            }
          },
          smooth: true,
          type: 'line',
          data: counts || defaultCounts,
          animationDuration: 2000,
          animationEasing: 'cubicInOut'
        },
        {
          name: '审核通过',
          smooth: true,
          type: 'line',
          itemStyle: {
            normal: {
              color: '#67C23A',
              lineStyle: {
                color: '#67C23A',
                width: 3
              },
              areaStyle: {
                color: 'rgba(103, 194, 58, 0.1)'
              }
            }
          },
          data: approved || defaultApproved,
          animationDuration: 2000,
          animationEasing: 'quadraticOut'
        }]
      })

      // 保存图表实例供父组件使用
      if (!window.lineChartInstance) {
        window.lineChartInstance = this.chart
      }
    }
  }
}
</script>
